package cn.com.ylpw.web.special.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import cn.com.ylpw.web.special.service.ISpecialService;
import cn.com.ylpw.web.util.AbstractService;

@Service("specialService")
public class SpecialServiceImpl extends AbstractService implements
		ISpecialService {

	 public final Logger log = LoggerFactory.getLogger(getClass());
	 private final static String NAME_SPACE="cn.com.ylpw.web.special.Special";

	@Override
	public Integer hasLottery(Integer customersid, String lotteryName) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("customersId", customersid);
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "hasLottery", params);
	}

	@Override
	public List<Map<String, Object>> findLotteryByName(String lotteryName) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "findLotteryByName", params);
	}

	@Override
	public List<Map<String, Object>> findLotteryLuckAll(Integer lotteryAreaId,
			String lotteryName) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("lotteryAreaId", lotteryAreaId);
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "findLotteryLuckAll", params);
	}

	@Override
	public Integer findLotteryNum(Integer customersid, String lotteryName,boolean currentDate) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("customersId", customersid);
		params.put("lotteryName", lotteryName);
		params.put("currentDate", currentDate);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findLotteryNum", params);
	}

	@Override
	public List<Map<String, Object>> findLotteryByCustomer(Integer customersid,
			String lotteryName) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("customersId", customersid);
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findLotteryByCustomer", params);
	}

	@Override
	public Map<String, Object> findLotteryByNameAndId(String lotteryName,
			Object productId) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("productId", productId);
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findLotteryByNameAndId", params);
	}

	@Override
	public Map<String, Object> findDefaultLotteryByName(String lotteryName,
			String productName) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("productName", productName);
		params.put("lotteryName", lotteryName);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findDefaultLotteryByName", params);
	}

	@Override
	public Integer savelottery(Integer customersId, int i,
			Integer zhongjiang, Integer id, Integer lotteryid) {
		Map<String,Object> lotterycustomers= Maps.newHashMap();
		lotterycustomers.put("customersId", customersId);
		lotterycustomers.put("integral", i);
		lotterycustomers.put("lotteryId", lotteryid);
		lotterycustomers.put("issucceed",zhongjiang);
		if(zhongjiang==1){
			lotterycustomers.put("productId", id);
		}else{
			lotterycustomers.put("productId", null);
		}
		Integer lotterycustomersId = saveLotterycustomers(lotterycustomers);
		if(zhongjiang == 1 && id != null && id.intValue()!=0){
			Map<String,Object> jfproduct = findJFProductById(id);
			int num = Integer.parseInt(jfproduct.get("num").toString());
			if(num-1<=0){
				jfproduct.put("display", 0);
				updateJFLotteryById(lotteryid);
			}
			jfproduct.put("num", num-1);
			updateJFProductById(jfproduct);
			
		}
		return lotterycustomersId;
	}

	@Override
	public Integer saveLotterycustomers(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().createObject(NAME_SPACE, "saveLotterycustomers", params);
	}

	@Override
	public Map<String, Object> findJFProductById(Integer productId) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("productId", productId);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findJFProductById", params);
	}

	@Override
	public void updateJFLotteryById(Integer lotteryId) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("lotteryId", lotteryId);
		super.superDao.getMyBatisFactory().updateObject(NAME_SPACE, "updateJFLotteryById", params);
	}

	@Override
	public void updateJFProductById(Map<String, Object> params) {
		super.superDao.getMyBatisFactory().updateObject(NAME_SPACE, "updateJFProductById", params);
	}

	@Override
	public Integer findlotteryId(Integer productId) {
		final Map<String,Object> params = Maps.newHashMap();
		params.put("productId", productId);
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findlotteryId", params);
	}

	@Override
	public Integer saveTest() {
		final Map<String,Object> params = Maps.newHashMap();
		return super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "saveTest", params);
	}

	@Override
	public Map<String, Object> updateCoupons(Map<String, Object> params)
			throws Exception {
		String cids[] = params.get("couponId").toString().split(",");
		params.put("cashcouponid", cids[0]);
		Integer count = super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "getCurCouponCount", params);
		Map<String,Object> result = Maps.newHashMap();
		if(count > 0){
			log.info("今天已经领取。。。{}客户ID：{}",new Date().getTime(),params.get("customerId"));
			result.put("success", false);
			result.put("msg", params.get("curMsg"));
			return result;
		}
		try{
			for(String cid : cids) {
				params.put("couponId", cid);
				Integer infoId = super.superDao.getMyBatisFactory().getObject(NAME_SPACE, "findCashCouponInfoById", params);
				if(infoId == null||infoId.intValue() <=0){
					throw new Exception("noQty");
				}
				params.put("cashcouponinfoid", infoId);
				super.superDao.getMyBatisFactory().updateObject(NAME_SPACE, "updateCashcouponInfo", params);
			}
		}catch (Exception e) {
			log.warn("现金券数量不足。。",e);
			if(params.get(e.getMessage())!=null){
				throw new Exception(params.get(e.getMessage()).toString());
			}
			throw new Exception("领取异常");
		}
		result.put("success", true);
		result.put("msg", params.get("successMsg"));
		return result;
	}

	
	
}
